//
//  DateTableviewController.m
//  MY QQapp
//
//  Created by liuchengming on 16/11/2.
//  Copyright © 2016年 liuchengming. All rights reserved.
//

#import "DateTableviewController.h"
#import "LoginData.h"

@interface DateTableviewController ()
{
    NSArray<LoginItemData *> * datas;
}

@end

@implementation DateTableviewController

- (void)viewDidLoad {
    [super viewDidLoad];
    datas = [NSArray new];
    //http://xl18z.cn/list.php?page=1  page = 页数
    NSURL * url = [NSURL URLWithString:@"http://xl18z.cn/list.php?page=1"];
    NSURLRequest * req = [NSURLRequest requestWithURL:url];
    NSURLSession * session = [NSURLSession sharedSession];
    NSURLSessionTask * task =[session dataTaskWithRequest:req completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
    {
        //NSString * text = [[NSString alloc] initWithBytes:data.bytes length:data.length encoding:NSUTF8StringEncoding];
       // NSLog(@"%@",text);
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        LoginData * d = [[LoginData alloc]initWithDictionary:dict];
        
        datas = d.record;
        NSLog(@"datas:%@",datas);
        dispatch_async(dispatch_get_main_queue() ,^
        {
            
            [self.tableView reloadData];
        });
        
    }];
    [task resume];
    //[self.tableView reloadData];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return datas.count + 1 ;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    LoginItemData * item = datas[indexPath.row];
    UITableViewCell * cell = [UITableViewCell new];
    
    if(indexPath.row < datas.count)
    {
        cell = [tableView dequeueReusableCellWithIdentifier:@"style1_2" forIndexPath:indexPath];
        cell.textLabel.text = item.name;
        cell.detailTextLabel.text = item.tel;
        int s;
        NSLog(@"99999999999999999999%i",s++);
        }else{
        cell = [tableView dequeueReusableCellWithIdentifier:@"nodata" forIndexPath:indexPath];
        
    }
    return cell;
}

//点击单元格
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIViewController * view = [self.storyboard instantiateViewControllerWithIdentifier:@"QQcell"];
    [self.navigationController pushViewController:view animated:YES];
}

/*
-(nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *) tableView editActionsForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    UITableViewRowAction * a1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"DEL" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath)
    {
        //[datas removeObjectAtIndex:indexPath.row];
        //[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
        //[tableView reloadData];
    }];
    
    UITableViewRowAction * a2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"普通" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath)
    {
        a2.backgroundColor = [UIColor blueColor];
    }];
    
    UITableViewRowAction * a3 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"默认" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath)
    {
        a3.backgroundColor = [UIColor greenColor];
    }];
    
    return @[a1,a2,a3];
}
*/


// Default is 1 if not implemented
/*
 - (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;    // fixed font style. use custom view (UILabel) if you want something different
 - (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
 
 // Editing
 
 // Individual rows can opt out of having the -editing property set for them. If not implemented, all rows are assumed to be editable.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
 */



/*
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [datas removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
}
*/
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
